

#include	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"
#include	"Function.h"
#include	"Display.h"
#include	"USB.h"
#ifdef  LC786XXX
#include	"LC786XXX.h"
#endif

UI_PLAY_INFO	sUSB_PlayInfo;
unsigned char	ucUSB_Resume;
UI_REPEAT_MODE	ucUSB_Repeat;
UI_RANDOM_MODE	ucUSB_Random;
USB_STATUS		ucUSB_Status;
unsigned char	ucUSB_Pause;
UI_INFO_MODE	ucUSB_DisplayInfo;
UI_ID3TAG_MODE	ucUSB_Display_ID3Tag;
unsigned char	ucUSB_TextStatus;
UI_SCAN_MODE	ucUSB_Scan;
unsigned short	usUSB_ScanFileNumber;
unsigned char	ucUSB_FastPlay;
unsigned char	ucUSB_ErrorCode;

unsigned char	ucUSB_FileName[MEDIA_CHAR_SIZE];
unsigned char	ucUSB_FileName_CharCode;
unsigned char	ucUSB_FolderName[MEDIA_CHAR_SIZE];
unsigned char	ucUSB_FolderName_CharCode;
unsigned char	ucUSB_ID3_Title[MEDIA_CHAR_SIZE];
unsigned char	ucUSB_ID3_Title_CharCode;
unsigned char	ucUSB_ID3_Artist[MEDIA_CHAR_SIZE];
unsigned char	ucUSB_ID3_Artist_CharCode;
unsigned char	ucUSB_ID3_Album[MEDIA_CHAR_SIZE];
unsigned char	ucUSB_ID3_Album_CharCode;

#ifdef  FILE_SEARCH
unsigned char	ucUSB_FileSearchName[MEDIA_CHAR_SIZE];
unsigned char	ucUSB_FileSearchName_CharCode;
extern void LC786XXX_GetFileNameStart(unsigned short ucNum);
extern void LC786XXX_FileSearch_CopyFileNameUSB(void);
extern void LC786XXX_FileSearchPlay(void);
#endif

extern void RANDOM_SetRandom(unsigned short usTotalFile, unsigned short usFirstFile);
extern unsigned short RANDOM_GetNextPlayFileNo(void);
extern LC786X_FORMAT	ucLC786X_FileFormat_USB;

#ifdef  LC786XXX
extern unsigned char ucLC786XXX_RandomFolderSkip;
extern LC786X_MEDIA_STATUS  ucLC786XXX_USB_Status;
extern unsigned short usLC786XXX_USB_TotalFolder;
extern unsigned char LC786XXX_PreviousCondition(void);
extern unsigned char LC786XXX_SkipFile(unsigned char ucDir);
extern void LC786XXX_SkipFolder(unsigned char ucDir);
extern void LC786XXX_FastPlayback(unsigned char ucDir);
extern void LC786XXX_ScanFileChange(void);
extern void LC786XXX_MoveToStartOfTrack(void);

extern unsigned char DRV_LC786XXX_Playback(LC786X_MEDIA_ID	ucMediaID, unsigned char ucMode);
extern unsigned char DRV_LC786XXX_Set_Repeat(unsigned char ucMode);
extern unsigned char DRV_LC786XXX_PlayOrder(void);
extern void DRV_LC786XXX_SetPlayOrder(unsigned char ucMode);
extern unsigned char DRV_LC786XXX_GetPlayOrder(void);
extern unsigned char DRV_LC786XXX_GetTotalMediaFile(unsigned char ucMediaID, unsigned char ucMode, unsigned short usFolderNumber);

extern unsigned char DRV_LC786XXX_PlayFileNumber(LC786X_MEDIA_ID ucMediaID, UI_PLAY_INFO sPlayInfo);
extern unsigned char LC786XXX_CurrentSoundOut(UI_FUNCTION ucFunc);

extern unsigned short usLC786XXX_USB_TotalFile;
extern unsigned short usLC786XXX_USB_TotalFileInFolder;
extern unsigned short usLC786XXX_USB_FirstFileNumberInFolder;
extern unsigned char ucLC786XXX_ErrorCode;

#endif

extern unsigned char ucDISP_FolderName;
extern unsigned char ucDISP_FileName;

extern void AUDIO_SetMute(unsigned char ucDes);

void USB_SetStatus(unsigned char ucDes)
{
	ucUSB_Status = ucDes;
}

void USB_SetTextStatus(unsigned char ucDes)
{
	ucUSB_TextStatus = ucUSB_TextStatus | ucDes;
}

void USB_ClearTextStatus(unsigned char ucDes)
{
	ucUSB_TextStatus = ucUSB_TextStatus & (~ucDes);
}

unsigned char USB_GetTextStatus(unsigned char ucDes)
{
	if  ((ucUSB_TextStatus & ucDes) == ucDes)
	{
		return(1);
	}
	return(0);
}

unsigned char USB_ClearFastPlayFlag(void)
{
	ucUSB_FastPlay = MEDIA_NORMAL_PLAY;
}

unsigned char USB_SoundStatus(void)
{
	if  (FUNC_GetCurrentFunction() == FUNC_USB)
	{
		return(LC786XXX_CurrentSoundOut(FUNC_USB));
	}
	return(1);
}

unsigned char USB_RandomFileChange(unsigned char ucMode)
{
	unsigned short usNextFileNo;
	UI_PLAY_INFO sPlayInfo;

	if  (ucUSB_FastPlay == MEDIA_FAST_BACKWARD_PLAY)
	{
		USB_FFREW_Release();
		return(0);
	}

	if  (ucMode == 0)
	{
		if  (LC786XXX_PreviousCondition() == 1)
		{
			LC786XXX_MoveToStartOfTrack();
			AUDIO_SetMute(0);
			return(0);
		}
	}
	DISP_PageScrollReset();
//	ucDISP_FileName = 1;
//	ucDISP_FolderName = 0;
	usNextFileNo = RANDOM_GetNextPlayFileNo();

//	if  (usNextFileNo != sUSB_PlayInfo.FileNo)
//	{
//		USB_ClearTextStatus(0xFF);
//	}
#ifdef  LC786XXX
	sPlayInfo.FileNo = usNextFileNo;
	sPlayInfo.FolderNo = 0;
	sPlayInfo.Hour = 0;
	sPlayInfo.Min = 0;
	sPlayInfo.Sec = 0;
	DRV_LC786XXX_PlayFileNumber(LC786X_USB, sPlayInfo);
#endif
	if  (ucUSB_FastPlay == MEDIA_FAST_FORWARD_PLAY)
	{
		USB_FastForwardStart();
	}
	return(1);
}

/*//////////////////////////////////////////////////////////////////
	Title : void USB_FileChange(unsigned char ucDes)
	ucDes
		-. 0 : down
		-. 1 : Up
//////////////////////////////////////////////////////////////////*/

unsigned char USB_FileChange(unsigned char ucDes)
{
	unsigned char ucRet = 1;
	
	USB_PauseOff(0);
	USB_ScanOff();
	if  ((ucUSB_Random == RANDOM_ALL) || (ucUSB_Random == RANDOM_FOLDER))
	{
		ucRet = USB_RandomFileChange(ucDes);
		return(ucRet);
	}

#ifdef  LC786XXX
	if  (LC786XXX_SkipFile(ucDes) == 0)
	{
		AUDIO_SetMute(0);
		DISP_PageScrollReset();
		ucRet = 0;
	}
	else
	{
	//	USB_ClearTextStatus(0xFF);
	//	ucDISP_FileName = 1;
	//	ucDISP_FolderName = 0;
	//	DISP_PageScrollReset();
		ucRet = 1;
	}
#endif

	return(ucRet);
}

#ifdef  FILE_SEARCH

void USB_FileSearchPlay(void)
{
	USB_ClearTextStatus(0xFF);
	USB_SetTextStatus(UI_TEXT_READY_FILENAME);
	LC786XXX_FileSearchPlay();
	LC786XXX_FileSearch_CopyFileNameUSB();
	ucDISP_FolderName = 0;
	ucDISP_FileName = 0;
//	ucUSB_FileName_CharCode = ucUSB_FileSearchName_CharCode;
//	LC786XXX_GetText_CopyText(ucUSB_FileName_CharCode, &ucUSB_FileSearchName[0], &ucUSB_FileName[0], usLC786XXX_GetText_Length);
}
/*//////////////////////////////////////////////////////////////////
	Title : void USB_FileSearch(unsigned char ucDes)
	ucDir :Direction
		-. 0 : down
		-. 1 : Up
	ucMode :
		-. 0 : Reset Search File Number
		-. 1 : Change Search File Number
//////////////////////////////////////////////////////////////////*/

unsigned short usUSB_FileSearchNumber;
unsigned char USB_FileSearch(unsigned char ucMode, unsigned char ucDir)
{
	if  (ucMode == 0)
	{
		usUSB_FileSearchNumber = sUSB_PlayInfo.FileNo;
	}

	if  (ucDir == 0)
	{
		if  (usUSB_FileSearchNumber > 1)
		{
			--usUSB_FileSearchNumber;
		}
		else
		{
			usUSB_FileSearchNumber = usLC786XXX_USB_TotalFile;
		}
	}
	else
	{
		if  (++usUSB_FileSearchNumber > usLC786XXX_USB_TotalFile)
		{
			usUSB_FileSearchNumber = 1;
		}
	}
	
	LC786XXX_GetFileNameStart(usUSB_FileSearchNumber);
	ucDISP_FolderName = 0;	
}
#endif
void USB_RandomOff(void)
{
	if  (ucUSB_Random != RANDOM_NONE)
	{
		ucUSB_Random = RANDOM_NONE;
		DRV_LC786XXX_SetPlayOrder(DRV_LC786XXX_GetPlayOrder() & 0xFC);
		DRV_LC786XXX_PlayOrder();
	}
}

void USB_PlayModeInit(void)
{
	ucUSB_Random = RANDOM_NONE;
	ucUSB_Scan = SCAN_NONE;
	ucUSB_Pause = 0;
}

/*//////////////////////////////////////////////////////////////////
	Title : void USB_FolderChange(unsigned char ucDes)
	ucDes
		-. 0 : down
		-. 1 : Up
//////////////////////////////////////////////////////////////////*/
void USB_FolderChange(unsigned char ucDes)
{
	USB_PauseOff(0);
	USB_ScanOff();
	if  (ucUSB_Random == RANDOM_NONE)
	{
		#ifdef  LC786XXX
		LC786XXX_SkipFolder(ucDes);
		#endif
	}
	else
	{
		unsigned short usFolderNumber;
		
		ucLC786XXX_RandomFolderSkip = ucUSB_Random;
		usFolderNumber = sUSB_PlayInfo.FolderNo;

		if  (ucDes == 0)	// Down
		{
			if  (usFolderNumber == 1)
			{
				usFolderNumber = usLC786XXX_USB_TotalFolder;
			}
			else
			{
				--usFolderNumber;
			}
		}
		else
		{
			++usFolderNumber;

			if  (usFolderNumber > usLC786XXX_USB_TotalFolder)
			{
				usFolderNumber = 1;
			}
		}
		DRV_LC786XXX_GetTotalMediaFile(LC786X_USB,0x01,usFolderNumber);
	}
}

unsigned char USB_GetFastPlayStatus(void)
{
	return(ucUSB_FastPlay);
}

void USB_FastForwardStart(void)
{
	USB_PauseOff(1);
	USB_ScanOff();
	#ifdef  LC786XXX
	if  (ucUSB_Repeat == REPEAT_FILE)
	{
		DRV_LC786XXX_SetPlayOrder(0x40 | 0x3F);
	}
	if  (ucUSB_Repeat == REPEAT_FOLDER)
	{
		DRV_LC786XXX_SetPlayOrder(0x40 | 0x3C);
	}
	if  ((ucUSB_Random == RANDOM_FOLDER) || (ucUSB_Random == RANDOM_ALL))
	{
		DRV_LC786XXX_SetPlayOrder(0x40);
	}
	if  ((ucUSB_Random == RANDOM_NONE) && (ucUSB_Repeat == REPEAT_NONE))
	{
		DRV_LC786XXX_SetPlayOrder(0x40);
	}
	LC786XXX_FastPlayback(0x00);
	#endif

	ucUSB_FastPlay = MEDIA_FAST_FORWARD_PLAY;
}
void USB_FastBackwardStart(void)
{
	USB_PauseOff(1);
	USB_ScanOff();
	#ifdef  LC786XXX
	if  (ucUSB_Repeat == REPEAT_FILE)
	{
		DRV_LC786XXX_SetPlayOrder(0x40 | 0x3F);
	}
	if  (ucUSB_Repeat == REPEAT_FOLDER)
	{
		DRV_LC786XXX_SetPlayOrder(0x40 | 0x3C);
	}
	if  ((ucUSB_Random == RANDOM_FOLDER) || (ucUSB_Random == RANDOM_ALL))
	{
		DRV_LC786XXX_SetPlayOrder(0x40 | 0x20);
	}

	if  ((ucUSB_Random == RANDOM_NONE) && (ucUSB_Repeat == REPEAT_NONE))
	{
		DRV_LC786XXX_SetPlayOrder(0x40 | 0x20);
	}
	
	LC786XXX_FastPlayback(0x01);
	#endif
	ucUSB_FastPlay = MEDIA_FAST_BACKWARD_PLAY;
}
void USB_FFREW_Release(void)
{
	#ifdef  LC786XXX
	if  ((ucUSB_Repeat == REPEAT_FOLDER) || (ucUSB_Repeat == REPEAT_FILE))
	{
		DRV_LC786XXX_SetPlayOrder(0x40);
	}
	if  ((ucUSB_Random == RANDOM_FOLDER) || (ucUSB_Random == RANDOM_ALL))
	{
		DRV_LC786XXX_SetPlayOrder(0x43);
	}
	if  ((ucUSB_Random == RANDOM_NONE) && (ucUSB_Repeat == REPEAT_NONE))
	{
		DRV_LC786XXX_SetPlayOrder(0x40);
	}
	LC786XXX_FastPlayback(0x03);
	#endif
	ucUSB_FastPlay = MEDIA_NORMAL_PLAY;
	
	DISP_PageScrollReset();
}

/*///////////////////////////////////////////////////////////////
Title : void USB_PauseOff(unsigned char ucDes)
ucDes
  0 : Clear Flag Only
  1 : Clear Flag and Send command for play
////////////////////////////////////////////////////////////////*/
void USB_PauseOff(unsigned char ucDes)
{
	if  (ucUSB_Pause == 0)
	{
		return;
	}
	if  (ucDes == 1)
	{
		#ifdef  LC786XXX
		if	(LC786XXX_GetMediaStatus(FUNC_USB) == LC786X_MS_PAUSE)
		{
			DRV_LC786XXX_Playback(LC786X_USB, 0x01);
			ucUSB_Pause = 0;
		}
		#endif
	}
	else
	{
		ucUSB_Pause = 0;
	}
}

unsigned char USB_SetPausePlay(void)
{
	if  (ucUSB_Pause == 0)
	{
		if  (ucLC786XXX_USB_Status == LC786X_MS_PLAYBACK)
		{
		#ifdef  LC786XXX
			DRV_LC786XXX_Playback(LC786X_USB, 0x02);
		#endif
			ucUSB_Pause = 1;
		}
	}
	else
	{
		USB_PauseOff(1);
	}
	return(ucUSB_Pause);
}

void USB_RepeatOff(void)
{
	if  (ucUSB_Repeat != REPEAT_NONE)
	{
		DRV_LC786XXX_Set_Repeat(LC786X_RPT_ALL);
		ucUSB_Repeat = REPEAT_NONE;
	}	
}

void USB_SetRepeat(void)
{	
	USB_PauseOff(1);
	USB_ScanOff();
	USB_RandomOff();

	if  (++ucUSB_Repeat > REPEAT_FOLDER)
	{
		USB_RepeatOff();
	}	

	switch  (ucUSB_Repeat)
	{
		case REPEAT_NONE:
			DRV_LC786XXX_Set_Repeat(LC786X_RPT_ALL);
			break;
		case REPEAT_FILE:
			DRV_LC786XXX_Set_Repeat(LC786X_RPT_FILE);
			break;
		case REPEAT_FOLDER:
			DRV_LC786XXX_Set_Repeat(LC786X_RPT_FLD);
			break;
	}
}

void USB_SetRandom(void)
{	
	USB_PauseOff(1);
	USB_RepeatOff();
	USB_ScanOff();
#ifdef  LC786XXX
	DRV_LC786XXX_SetPlayOrder(DRV_LC786XXX_GetPlayOrder() | 0x03);
	DRV_LC786XXX_PlayOrder();
#endif

	if  (++ucUSB_Random > RANDOM_FOLDER)
	{
		USB_RandomOff();
	}

	#if  1
	switch  (ucUSB_Random)
	{
		case RANDOM_ALL:
			RANDOM_SetRandom(usLC786XXX_USB_TotalFile, 1);
			USB_RandomFileChange(1);
			break;
		case RANDOM_FOLDER:
			RANDOM_SetRandom(usLC786XXX_USB_TotalFileInFolder, usLC786XXX_USB_FirstFileNumberInFolder);
			USB_RandomFileChange(1);
			break;
	}
	
	#else
	if  (ucUSB_Random == RANDOM_ALL)
	{
		RANDOM_SetRandom(usLC786XXX_USB_TotalFile, 1);
	}
	else
	{
		RANDOM_SetRandom(usLC786XXX_USB_TotalFileInFolder, usLC786XXX_USB_FirstFileNumberInFolder);
	}
	#endif
}

void USB_ScanOff(void)
{
	if  (ucUSB_Scan != SCAN_NONE)
	{
		if  (ucUSB_Scan == SCAN_FOLDER)
		{
		#ifdef  LC786XXX
			DRV_LC786XXX_Set_Repeat(LC786X_RPT_ALL);
		#endif
		}
		ucUSB_Scan = SCAN_NONE;
	}
}

void USB_SetScan(void)
{
	USB_RepeatOff();
	USB_RandomOff();
	USB_PauseOff(1);

	switch  (ucUSB_Scan)
	{
		case SCAN_NONE:
			LC786XXX_ScanFileChange();
		//	USB_ClearTextStatus(0xFF);
			ucUSB_Scan = SCAN_ALL;
			usUSB_ScanFileNumber = sUSB_PlayInfo.FileNo;
		#ifdef  LC786XXX
			DRV_LC786XXX_Set_Repeat(LC786X_RPT_ALL);	// Set to Disc Repeat
		#endif
			break;
		case SCAN_FOLDER:
			USB_ScanOff();
		#ifdef  LC786XXX
			DRV_LC786XXX_Set_Repeat(LC786X_RPT_ALL);	// Set to Disc Repeat
		#endif
			break;
		case SCAN_ALL:
			ucUSB_Scan = SCAN_FOLDER;
			usUSB_ScanFileNumber = sUSB_PlayInfo.FileNo;
		#ifdef  LC786XXX
			DRV_LC786XXX_Set_Repeat(LC786X_RPT_FLD);	// Set to Folder Repeat
		#endif
			break;
	}
	LOG_USB("USB_SetScan:[M:%d]:[No:%d]",ucUSB_Scan,usUSB_ScanFileNumber);
}

/*//////////////////////////////////////////////////////////////////
void UI_USB_Info(unsigned short ucMode)
	0 : Short Key
	1 : Long Key, Tag Mode
*/////////////////////////////////////////////////////////////////////////////
void USB_SetInfo(unsigned short ucMode)
{
	USB_PauseOff(1);
	DISP_PageScrollReset();
	ucDISP_FolderName = 0;
	ucDISP_FileName = 0;
	if  (ucMode == 1)
	{
		if  (ucUSB_DisplayInfo == INFO_ID3TAG)
		{
			if  (++ucUSB_Display_ID3Tag > ID3TAG_BITRATE)
			{
				ucUSB_Display_ID3Tag = ID3TAG_TITLE;	
			}
		}
		else
		{
			ucUSB_DisplayInfo = INFO_ID3TAG;
		}
				
		return;
	}
	if  (++ucUSB_DisplayInfo > INFO_ID3TAG)
	{
		ucUSB_DisplayInfo = INFO_FILENAME;
	}
}

void USB_Variable_Initalize(void)
{
	sUSB_PlayInfo.FolderNo = 0;
	sUSB_PlayInfo.FileNo = 0;
	sUSB_PlayInfo.Hour = 0;
	sUSB_PlayInfo.Min = 0;
	sUSB_PlayInfo.Sec = 0;
	sUSB_PlayInfo.BitRate = 0;
	ucUSB_Resume = 0;
	ucUSB_Repeat = REPEAT_NONE;
	ucUSB_Random = RANDOM_NONE;
	ucUSB_Pause = 0;
	ucUSB_DisplayInfo = INFO_FILENAME;
	ucUSB_Display_ID3Tag = ID3TAG_TITLE;
	ucUSB_TextStatus = 0;
	ucUSB_Scan = SCAN_NONE;
	usUSB_ScanFileNumber = 0;
	memset(&ucUSB_FileName[0], 0x00, MEDIA_CHAR_SIZE);
	memset(&ucUSB_FolderName[0], 0x00, MEDIA_CHAR_SIZE);
	memset(&ucUSB_ID3_Title[0], 0x00, MEDIA_CHAR_SIZE);
	memset(&ucUSB_ID3_Artist[0], 0x00, MEDIA_CHAR_SIZE);
	memset(&ucUSB_ID3_Album[0], 0x00, MEDIA_CHAR_SIZE);
	ucUSB_FastPlay = MEDIA_NORMAL_PLAY;
	ucLC786X_FileFormat_USB = LC786X_FORMAT_UNKNOWN;
	USB_SetStatus(USB_ST_NONE);
	ucUSB_ErrorCode = LC786X_EC_NoError;
}


